package org.nus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
 * Action to validate user log in.
 * @author Xiaohui
 *
 */
public class LoginAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginForm loginform = (LoginForm) form;
		DBDisplayer displayer = new DBDisplayer();

		if (displayer.ValidateUser(loginform.getUsername().toLowerCase(),
				loginform.getPassword().toLowerCase())) {
			HttpSession session = request.getSession();
			User curruser = new User(loginform.getUsername(), loginform
					.getPassword());
			session.setAttribute("curruser", curruser);
			//All jps pages should use one DBDisplayer
			DBDisplayer dbplayer = new DBDisplayer();
			session.setAttribute("dbconn", dbplayer);
//			System.out.println("session user created");
			return mapping.findForward("success");
		}

		else {
			return mapping.findForward("failure");
		}
	}
}